package com.app.view.component.room 
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.display.Loader;
	
	import com.app.view.component.room.seat.Clock;
	import com.peachcastle.core.Display;
	import com.app.model.data.UserData;
	import com.app.view.component.resource.ResourceManager;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class SeatInfo extends Display
	{
		private var _data:UserData = null;
		
		private var _clock:Clock = null;
		
		private var _txt_name:TextField = null;
		private var _txt_chips:TextField = null;
		
		private var _avatar:MovieClip = null;
		
		public function SeatInfo(ui:MovieClip = null) 
		{
			super(ui, true);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_clock = new Clock(assest["clock_view"]);
			
			_txt_name = assest["txt_name"];
			_txt_chips = assest["txt_chips"];
			
			_avatar = assest["avatar"];
		}		
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
		}
		
		public function addPlayer(data:UserData = null):void
		{
			super.showAssestBase();
			
			_data = data;

			_txt_name.text = data.name;
			_txt_chips.text = String(data.sitChips);
			
			updateAvatar(data);
		}
		
		public function removePlayer():void
		{
			super.hideAssest();
			_data = null;
		}
		
		public function updateAvatar(data:UserData):void
		{
			if (_avatar.numChildren > 1) _avatar.removeChildAt(1);
		
			var loader:Loader = ResourceManager.getInstance().getObjectLoader(data.mid, "middle");
			
			if (loader)
			{
				_avatar.addChild(loader);
			}else
			{
				loader = ResourceManager.getInstance().addObjectLoader(data.mid, "middle", data.icon, 72, 1);
				_avatar.addChild(loader);
			}
		}
		
		public function update(data:UserData = null):void
		{
			if (data != null)
			{
				_data.name = data.name;
				_data.sitChips = data.sitChips;

				_txt_name.text = data.name;
				_txt_chips.text = String(data.sitChips);
			}
		}
		
		public function get clock():Clock { return _clock; }
	}

}